package cn.yunhe.controller;


import cn.yunhe.model.Member;
import cn.yunhe.model.Role;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpSession;
import javax.xml.ws.RequestWrapper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
@SessionAttributes(value = {"names","age"},types = {String.class})
public class HelloSpringMvcController {

    //http://localhost:8080/secondServlet.dojava
    @RequestMapping(value = "/hello444",method = RequestMethod.GET)
    public String helloSpringMvc(Member member, HttpSession httpSession){

        System.out.println("444getMname==="+member.getMname());
        System.out.println("444getMpwd===="+member.getMpwd());

        httpSession.setAttribute("member111",member);
        return "redirect:secondServlet.dojava";
    }


    @RequestMapping(value = "/secondServlet")
    public String memberInfo(HttpSession httpSession){
        Member member= (Member) httpSession.getAttribute("member111");
        System.out.println("555getMname==="+member.getMname());
        System.out.println("555getMpwd===="+member.getMpwd());

        return "hello222";
        //return "hello222111";
    }


    //http://localhost:8080/getById111.dojava?mid=23
    @RequestMapping(value = "/getById111",method = RequestMethod.GET)
    public ModelAndView getById(
            @RequestParam(name = "mid",required = true,defaultValue = "-1")
                    Long mid111){
        System.out.println("mid111===="+mid111);

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("mid111222",mid111);
        modelAndView.setViewName("hello333");
        return modelAndView;
    }

    //http://localhost:8080/getById111222.dojava?mid=53
    @RequestMapping(value = "/getById111222",method = RequestMethod.GET)
    public String getById111(
            @RequestParam(name = "mid",required = true,defaultValue = "-1")
                    Long mid111,HttpSession httpSession){
        System.out.println("mid111===="+mid111);
        httpSession.setAttribute("mid111222",mid111);
        return "hello444";
    }




    @RequestMapping(value = "login2getMemberInfo")
    public String getMemberInfo(Model model,Member member){
        System.out.println("666getMname==="+member.getMname());
        System.out.println("666getMpwd==="+member.getMpwd());

        model.addAttribute("mname111",member.getMname());
        model.addAttribute("mpwd111",member.getMpwd());

        return "redirect:memberInfo222Display.dojava";

    }

    //http://localhost:8080/memberInfo222Display.dojava?mname=%3F%3F%3F%3F&mpwd=111
    //接收参数(中文),中文乱码
    @RequestMapping(value = "/memberInfo222Display")
    public ModelAndView memberInfo222(String mname111,String mpwd111){
        System.out.println("777getMname111==="+mname111);
        System.out.println("777getMpwd111==="+mpwd111);

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setView(new MappingJackson2JsonView());
        modelAndView.addObject("mname222",mname111);
        modelAndView.addObject("mpwd222",mpwd111);

        return modelAndView;
    }

/*
*
*上述 在重定向的过程中，因为是以字符串传递的，不能传递POJO类
* 如果需要传递POJO,这个时候就可以使用如下方法
*
* */


    //@RequestMapping(value = "login2getMemberInfo")
    public String getMemberInfo(RedirectAttributes redirectAttributes, Member member){
        System.out.println("888getMname==="+member.getMname());
        System.out.println("888getMpwd==="+member.getMpwd());

        redirectAttributes.addFlashAttribute("member",member);

        return "redirect:memberInfo222Display.dojava";

    }

    //http://localhost:8080/memberInfo222Display.dojava?mname=%3F%3F%3F%3F&mpwd=111
    //中文没有乱码
    //@RequestMapping(value = "/memberInfo222Display")
    public ModelAndView memberInfo222(Member member){
        System.out.println("999getMname==="+member.getMname());
        System.out.println("999getMpwd==="+member.getMpwd());

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setView(new MappingJackson2JsonView());
        modelAndView.addObject("member999",member);

        return modelAndView;
    }


    //@RequestMapping("/testSession")
    public String testSession111(Map<String,Object>objectMap){
        System.out.println(Arrays.asList("王大","李二","张三"));
        objectMap.put("names", Arrays.asList("王大","李二","张三"));
        objectMap.put("age",25);

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject(objectMap);
        return "memberInfo2names2age";

    }

    //http://localhost:8080/testSession.dojava
    //如果没有@SessionAttributes声明,则只为requestScope作用域
    //@SessionAttributes(value = {"names","age"},types = {String.class,Integer.class})
    @RequestMapping("/testSession")
    public String testSession(Map<String,Object>objectMap){
        System.out.println(Arrays.asList("1王大","2李二","3张三"));
        objectMap.put("names", Arrays.asList("3王大","2李二","1张三"));//默认存入requestScope作用域
        objectMap.put("age",34);
        return "memberInfo2names2age";

    }


    //http://localhost:8080/memberInfo111.dojava
    //@RequestMapping(value = "/memberInfo111")
    public ModelAndView memberInfo1(){
        Member member=new Member(201800902,"王大","111","0");
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("member111",member);
        modelAndView.setViewName("memberInfo");//转发
        return modelAndView;
    }


    //http://localhost:8080/memberInfo222.dojava?mid111=45
    //@RequestMapping(value = "/memberInfo222",method = RequestMethod.GET)
    public ModelAndView memberInfo2(
            @RequestParam(name = "mid111",required = true,defaultValue = "-1")
                    int mid222){

        Member member=new Member(mid222,"王大333","333","3");
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("member111",member);
        modelAndView.setViewName("memberInfo");//转发
        return modelAndView;
    }


    //控制器返回JSON数据传到视图
    //http://localhost:8080/memberInfo333.dojava?mid111=33
    @RequestMapping(value = "/memberInfo333",method = RequestMethod.GET)
    public ModelAndView memberInfo(
            @RequestParam(name = "mid111",required = true,defaultValue = "-1")
                    int mid222){

        Member member=new Member(mid222,"王大333","333","3");
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("member111",member);
        //向前台传递JSON数据
        modelAndView.setView(new MappingJackson2JsonView());
        return modelAndView;
    }


    //http://localhost:8080/getURLParameters111/4.dojava
    //@RequestMapping(value = "/getURLParameters111/{mid111}")
    public ModelAndView getURLParameters1(@PathVariable("mid111")int mid222){
        System.out.println("mid222===="+mid222);

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("mid222222",mid222);
        modelAndView.setViewName("getURLParametersPage");
        return modelAndView;
    }

    //http://localhost:8080/getURLParameters111/333.dojava
    @RequestMapping(value = "/getURLParameters111/{mid111}")
    public ModelAndView getURLParameters(@PathVariable("mid111")int mid222){
        System.out.println("mid222===="+mid222);

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("getURLParametersPage");
        return modelAndView;
    }


    //前台==>后台的Ajax请求,及模型驱动(ModelDriven)的参数自动绑定
    //后台==>前台的Ajax请求结果集JSON格式,
    //http://localhost:8080/ZViewTest/getAjax2Json.jsp
    @RequestMapping(value = "/getAjax2JsonJsp111")
    public ModelAndView getAjax2JsonJsp(@RequestBody(required = true)Member member){
        System.out.println("getMid======"+member.getMid());
        System.out.println("getMname======"+member.getMname());
        System.out.println("getMpwd======"+member.getMpwd());
        System.out.println("getMflag======"+member.getMflag());
        System.out.println("getTotalRecords======"+member.getPagination().getTotalRecords());
        System.out.println("getMaxRows======"+member.getPagination().getMaxRows());
        System.out.println("getCurrPageNo======"+member.getPagination().getCurrPageNo());

        Role role1=new Role(1,"王大","111","1");
        Role role2=new Role(2,"李二","222","2");
        Role role3=new Role(3,"张三","333","3");

        List<Role>roleList=new ArrayList<>();
        roleList.add(role1);
        roleList.add(role2);
        roleList.add(role3);

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject(roleList);
        modelAndView.setView(new MappingJackson2JsonView());
        return modelAndView;

    }

    //http://localhost:8080/ZViewTest/getAjax2Json.jsp
    @RequestMapping(value = "/deleteById111")
    public ModelAndView deleteById(@RequestBody(required = true)List<Integer> ridList){
        for (Integer rid:ridList) {
            System.out.println("rid=="+rid);
        }

        System.out.println("ridList.size()===="+ridList.size());
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("length",ridList.size());
        modelAndView.setView(new MappingJackson2JsonView());
        return modelAndView;
    }

    //http://localhost:8080/ZViewTest/getAjax2Json.jsp
    @RequestMapping(value = "/deleteByRole111")
    public ModelAndView deleteByRole(@RequestBody(required = true)List<Role>roleList){
        for (int i=0;i<roleList.size();i++){
            Role role=roleList.get(i);
            System.out.print(role.getRid()+"\t");
            System.out.print(role.getRname()+"\t");
            System.out.print(role.getRpwd()+"\t");
            System.out.print(role.getRflag()+"\t");
            System.out.println();
        }

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("length2RoleList",roleList.size());
        modelAndView.setView(new MappingJackson2JsonView());
        return modelAndView;

    }

    //属性驱动
    //http://localhost:8080/ZViewTest/getAjax2Json.jsp
    //@RequestMapping(value = "/serializable2Role111")
    public ModelAndView serializable2Role1(String rname,String rpwd){
        System.out.println("rname====="+rname);
        System.out.println("rpwd====="+rpwd);

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("rname111",rname);
        modelAndView.addObject("rpwd111",rpwd);
        modelAndView.setViewName("roleInfo");
        return modelAndView;

    }


    //模型驱动
    //http://localhost:8080/ZViewTest/getAjax2Json.jsp
    @RequestMapping(value = "/serializable2Role111")
    public ModelAndView serializable2Role(Role role){
        System.out.println("rname====="+role.getRname());
        System.out.println("rpwd====="+role.getRpwd());

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("rname111",role.getRname());
        modelAndView.addObject("rpwd111",role.getRpwd());
        modelAndView.setViewName("roleInfo");
        return modelAndView;

    }



    //直接点击如下链接可跳转
    //http://localhost:8080/getHeaderAndCookies111.dojava
    //获得特定设备的Cookies和请求头
    @RequestMapping(value = "getHeaderAndCookies111")
    public String getHeaderAndCookies(
            @RequestHeader(value = "User-Agent",required = false,defaultValue = "attribute")String userAgent222,
            @CookieValue(value = "JSESSIONID",required = true,defaultValue = "myidesJsessionId")String mySessionId222
    ){

        System.out.println("userAgent222===="+userAgent222);
        System.out.println("mySessionId222===="+mySessionId222);

        return "login";
    }

    

}
